﻿using RenderHeads.Media.AVProVideo;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class AVProVideoPlayerMgr : MonoSingleton<AVProVideoPlayerMgr> {

    public System.Action ready_callback;

    MediaPlayer mediaplayer;

    public bool _noDefaultDisplay = true;

    public Texture _defaultTexture;

    bool isready;

    public MediaPlayer Mediaplayer
    {
        get
        {
            if (mediaplayer == null)
            {
                mediaplayer = Utils.GetSafeComponet<MediaPlayer>(gameObject);
            }
            return mediaplayer;
        }
    }

    public bool IsReady
    {
        get {
            return this.isready;
        }
    }

    public bool IsMute
    {
        set { if(mediaplayer.Control != null) mediaplayer.Control.MuteAudio(value); }
        get { return mediaplayer.Control.IsMuted(); }
    }

    public int Width {
        get { return mediaplayer.Info.GetVideoWidth(); }
    }

    public int Height
    {
        get { return mediaplayer.Info.GetVideoHeight(); }
    }

    public float CurrentFrameRate
    {
        get {
            if (mediaplayer.Info != null) return mediaplayer.Info.GetVideoPlaybackRate();
            return -1f;
        }
    }

    public string CurrentVideoName
    {
        get {
            string m_VideoPath = mediaplayer.m_VideoPath;
            return Path.GetFileNameWithoutExtension(m_VideoPath);
        }
    }

    public bool IsPlaying
    {
        get {
            if (mediaplayer.Control == null) return false;
            return mediaplayer.Control.IsPlaying();
        }
    }

    public Texture mainTexture
    {
        get
        {
            Texture result = Texture2D.whiteTexture;
            if (HasValidTexture())
            {
                result = mediaplayer.TextureProducer.GetTexture();
            }
            else
            {
                if (_noDefaultDisplay)
                {
                    result = null;
                }
                else if (_defaultTexture != null)
                {
                    result = _defaultTexture;
                }
            }
            return result;
        }
    }

    public float CurrentTimeMs
    {
        get {
            if (mediaplayer.Control == null) return 0;
            return mediaplayer.Control.GetCurrentTimeMs();
        }
    }

    public float Progress
    {
        get
        {
            if (mediaplayer.Control == null) return 0;

            float tVideoTime = mediaplayer.Info.GetDurationMs();

            if (tVideoTime > 0)
            {
                float tCurrentTime = mediaplayer.Control.GetCurrentTimeMs();
                return tCurrentTime / tVideoTime;
            }

            return 0;
        }
        set {

            if (mediaplayer.Control == null) return;

            if (value > 1) value = 1;
            if (value < 0) value = 0;

            float tVideoTime = mediaplayer.Info.GetDurationMs();        //当前视频的时间
            if (tVideoTime > 0)
            {
                float tCurrentTime = value * tVideoTime;
                //将视频时间调到对应的节点
                mediaplayer.Control.Seek(tCurrentTime);
            }
            else
            {
                Debug.Log("当前没有视频在播放");
            }
        }
    }

    public void SeekMs(float time)
    {
        if (mediaplayer.Control == null) return;
        mediaplayer.Control.Seek(time);
    }

    public float Volume
    {
        get {
            return mediaplayer.Control.GetVolume();
        }
        set {
            mediaplayer.Control.SetVolume(value);
        }
    }

    protected override void Init()
    {
        base.Init();

        mediaplayer = Utils.GetSafeComponet<MediaPlayer>(gameObject);

    }

    private void Update()
    {
        if (!isready && HasValidTexture())
        {
            if (ready_callback != null) ready_callback();
            isready = true;
        } 
    }

    public bool PlayVideo(MediaPlayer.FileLocation filelocation, string path,bool is_auto_play = false)
    {
        isready = false;
       return mediaplayer.OpenVideoFromFile(filelocation, path, is_auto_play);
    }

    public void PlayOrPause()
    {
        if (mediaplayer.Control.IsPlaying())
            Pause();
        else if (mediaplayer.Control.IsPaused())
            Play();
    }

    public void Play()
    {
        if(mediaplayer.Control != null)
            mediaplayer.Control.Play();
    }

    public void Pause()
    {
        if(mediaplayer.Control != null)
            mediaplayer.Control.Pause();
    }

    public void Stop()
    {
        if(mediaplayer.Control != null) mediaplayer.Control.Stop();
    }

    public bool HasValidTexture()
    {
        return (mediaplayer != null && mediaplayer.TextureProducer != null && mediaplayer.TextureProducer.GetTexture() != null);
    }

    public Texture2D CapturePicture()
    {
        Debug.Log("开始截图");
        if (HasValidTexture())
        {
            Texture2D tempt2d = mediaplayer.TextureProducer.GetTexture() as Texture2D;
            Texture2D targett2d = null;
            targett2d = Helper.GetReadableTexture(tempt2d, mediaplayer.TextureProducer.RequiresVerticalFlip(), targett2d);
            return targett2d;
        }
        return null;
    }

}
